/*
Copyright (c) 2012 the VATSIM Connector project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef __XPINN__PLANESDB_H__
#define __XPINN__PLANESDB_H__

#include <string>
#include <vector>
#include <list>
#include <map>
#include <functional>
#include <iostream>
#include <memory>
#include "xpinn.h"

#if IBM
#pragma warning( disable : 4251 )
#endif

class DEBUG_EXPORT XPInnPlanesTaxonomy
{
public:
  void reloadDesignators(std::string const& filename);

  void reloadDesignators(std::istream& file, std::string const& filename);

  void reloadPlanes(std::string const& filename);

  void reloadPlanes(std::istream& file, std::string const& filename);

  std::string getClosestMatch(std::string const& type, std::string const& airline);

  void visitDesignators(std::function<void(std::string const&, std::list<std::string> const&, unsigned)> const&) const;

  void visitPlanes(std::function<void(std::string const&, std::list<std::string> const&)> const&) const;

private:
  void parseDesignator(std::string const& text, std::string const& filename, int line);
  void parsePlane(std::string const& text, std::string const& filename, int line);

  struct Designator
  {
    Designator() : m_minmatch(0) {}
    Designator(std::list<std::string> const& taxa, unsigned minmatch) : m_taxa(taxa), m_minmatch(minmatch) {}
    std::list<std::string> m_taxa;
    unsigned m_minmatch;
  };
  typedef std::map<std::string, Designator> map_type;
  map_type m_designators;

  template <typename K, typename T>
  class Tree
  {
  public:
    Tree() {}
    Tree(T const& obj) : m_obj(obj) {}
    void set(T const& value) { m_obj = value; }
    T& get() { return m_obj; }
    T const& get() const { return m_obj; }
    void clear() { m_obj = std::string(); m_map.clear(); }
    void visit(std::function<void(T const&, std::list<K> const&)> const& fn, std::list<K> keys = std::list<K>()) const
    {
      fn(get(), keys);
      for (std::map<K, Tree>::const_iterator it = m_map.begin(); it != m_map.end(); ++it)
      {	std::list<K> newkeys (keys); newkeys.push_back(it->first); it->second.visit(fn, newkeys); }
    }
    void insert(std::list<K> const& keys, T const& value)
    {
      if (keys.size() > 0)
	m_map.insert(std::map<K, Tree>::value_type(keys.front(), value))
	  .first->second.insert(std::list<K>(++keys.begin(), keys.end()), value);
      else
	set(value);
    }
    Tree& descend(std::list<K>& keys)
    {
      if (keys.size() > 0 && m_map.count(keys.front()) > 0)
      {
	K front = keys.front();
	keys.pop_front();
	return m_map[front].descend(keys);
      }
      else return *this;
    }
    Tree const& descend(std::list<K>& keys) const { return const_cast<Tree*>(this)->descend(keys); }
  private:
    T m_obj;
    std::map<K, Tree> m_map;
  };
  typedef Tree<std::string, std::string> tree_type;
  tree_type m_tree;
};

/*
		o  0: root
	       /|\
	      / | \
	     o  o  o  1: liveries ("UAL", "BAW", ...)
	    /|  |  |\
	   / |  |  | \
	  o  o  o  o  o  2: descriptions ("Landplane", "SeaPlane", "Helicopter", ...)
	 /  /|  |  |\  \
	/  / |  |  | \  \
       o  o  o  o  o  o  o  3: wake categories ("L", "M", "H")
      /  /  /  /|\  \  \  \
     /  /  /  / | \  \  \  \
    o  o  o  o  o  o  o  o  o  4: engine types ("Jet", "Piston", "Turboprop", "Electric")
   /  /  /|    /|\    |\  \  \
  /  /  / |   / | \   | \  \  \
 o  o  o  o  o  o  o  o  o  o  o  5: engine counts ("1", ..., "8")
   /|  |    /  /|\  \    |  |\
  / |  |   /  / | \  \   |  | \
 o  o  o  o  o  o  o  o  o  o  o  6: familes (e.g. "B737")
   /  /|\    |    /|\    |\    |
  /  / | \   |   / | \   | \   |
 o  o  o  o  o  o  o  o  o  o  o  7: types (e.g. "B733", "B738", ...)
*/

#endif //__XPINN__PLANESDB_H__